package com.windward.vii.dao.mapper;

import com.windward.vii.dao.entity.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface OrderMapper {
    @Delete({
        "delete from payment_order",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into payment_order (id, sn, uid, ",
        "amount, coin, status, ",
        "pay_type, pay_status, ",
        "pay_time, deliver_status, ",
        "deliver_time, remark, ",
        "create_time, update_time, ",
        "expire_time, goods_id, ",
        "goods_sn, goods_price, ",
        "goods_coin, goods_num, ",
        "goods_name, goods_icon, ",
        "out_trade_no, ip, ",
        "terminal_type)",
        "values (#{id,jdbcType=BIGINT}, #{sn,jdbcType=VARCHAR}, #{uid,jdbcType=BIGINT}, ",
        "#{amount,jdbcType=INTEGER}, #{coin,jdbcType=INTEGER}, #{status,jdbcType=TINYINT}, ",
        "#{payType,jdbcType=TINYINT}, #{payStatus,jdbcType=TINYINT}, ",
        "#{payTime,jdbcType=TIMESTAMP}, #{deliverStatus,jdbcType=TINYINT}, ",
        "#{deliverTime,jdbcType=TIMESTAMP}, #{remark,jdbcType=VARCHAR}, ",
        "#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, ",
        "#{expireTime,jdbcType=TIMESTAMP}, #{goodsId,jdbcType=BIGINT}, ",
        "#{goodsSn,jdbcType=VARCHAR}, #{goodsPrice,jdbcType=INTEGER}, ",
        "#{goodsCoin,jdbcType=INTEGER}, #{goodsNum,jdbcType=INTEGER}, ",
        "#{goodsName,jdbcType=VARCHAR}, #{goodsIcon,jdbcType=VARCHAR}, ",
        "#{outTradeNo,jdbcType=VARCHAR}, #{ip,jdbcType=VARCHAR}, ",
        "#{terminalType,jdbcType=TINYINT})"
    })
    int insert(Order record);

    int insertSelective(Order record);

    @Select({
        "select",
        "id, sn, uid, amount, coin, status, pay_type, pay_status, pay_time, deliver_status, ",
        "deliver_time, remark, create_time, update_time, expire_time, goods_id, goods_sn, ",
        "goods_price, goods_coin, goods_num, goods_name, goods_icon, out_trade_no, ip, ",
        "terminal_type",
        "from payment_order",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    Order selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Order record);

    @Update({
        "update payment_order",
        "set sn = #{sn,jdbcType=VARCHAR},",
          "uid = #{uid,jdbcType=BIGINT},",
          "amount = #{amount,jdbcType=INTEGER},",
          "coin = #{coin,jdbcType=INTEGER},",
          "status = #{status,jdbcType=TINYINT},",
          "pay_type = #{payType,jdbcType=TINYINT},",
          "pay_status = #{payStatus,jdbcType=TINYINT},",
          "pay_time = #{payTime,jdbcType=TIMESTAMP},",
          "deliver_status = #{deliverStatus,jdbcType=TINYINT},",
          "deliver_time = #{deliverTime,jdbcType=TIMESTAMP},",
          "remark = #{remark,jdbcType=VARCHAR},",
          "create_time = #{createTime,jdbcType=TIMESTAMP},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP},",
          "expire_time = #{expireTime,jdbcType=TIMESTAMP},",
          "goods_id = #{goodsId,jdbcType=BIGINT},",
          "goods_sn = #{goodsSn,jdbcType=VARCHAR},",
          "goods_price = #{goodsPrice,jdbcType=INTEGER},",
          "goods_coin = #{goodsCoin,jdbcType=INTEGER},",
          "goods_num = #{goodsNum,jdbcType=INTEGER},",
          "goods_name = #{goodsName,jdbcType=VARCHAR},",
          "goods_icon = #{goodsIcon,jdbcType=VARCHAR},",
          "out_trade_no = #{outTradeNo,jdbcType=VARCHAR},",
          "ip = #{ip,jdbcType=VARCHAR},",
          "terminal_type = #{terminalType,jdbcType=TINYINT}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Order record);

    /***********以下为自定义方法************************/

    @Select({
            "select",
            "id, sn, uid, amount, coin, status, pay_type, pay_status, pay_time, deliver_status, ",
            "deliver_time, remark, create_time, update_time, expire_time, goods_id, goods_sn, ",
            "goods_price, goods_coin, goods_num, goods_name, goods_icon, out_trade_no, ip, ",
            "terminal_type",
            "from payment_order",
            "where sn = #{sn,jdbcType=VARCHAR}"
    })
    @ResultMap("BaseResultMap")
    Order selectBySn(String sn);

    @Select({
            "select",
            "id, sn, uid, amount, coin, status, pay_type, pay_status, pay_time, deliver_status, ",
            "deliver_time, remark, create_time, update_time, expire_time, goods_id, goods_sn, ",
            "goods_price, goods_coin, goods_num, goods_name, goods_icon, out_trade_no, ip, ",
            "terminal_type",
            "from payment_order",
            "where out_trade_no = #{outTradeNo,jdbcType=VARCHAR}"
    })
    @ResultMap("BaseResultMap")
    Order selectByOutTradeNo(String outTradeNo);

    @Select({
            "select",
            "id, sn, uid, amount, coin, status, pay_type, pay_status, pay_time, deliver_status, ",
            "deliver_time, remark, create_time, update_time, expire_time, goods_id, goods_sn, ",
            "goods_price, goods_coin, goods_num, goods_name, goods_icon, out_trade_no, ip, ",
            "terminal_type",
            "from payment_order",
            "where uid = #{uid,jdbcType=BIGINT} and pay_status = 2"
    })
    @ResultMap("BaseResultMap")
    List<Order> getUserOrderPageList(Long uid);

    // 配送：更新订单状态，deliver_status = 1 保证多线程处理问题。
    @Update({
            "update payment_order",
            "set status = #{status,jdbcType=TINYINT},",
            "deliver_status = #{deliverStatus,jdbcType=TINYINT},",
            "deliver_time = #{deliverTime,jdbcType=TIMESTAMP},",
            "update_time = #{updateTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT} and deliver_status = 1"
    })
    int update4Deliver(Order record);

    @Select({
            "select",
            "id, sn, uid, amount, coin, status, pay_type, pay_status, pay_time, deliver_status, ",
            "deliver_time, remark, create_time, update_time, expire_time, goods_id, goods_sn, ",
            "goods_price, goods_coin, goods_num, goods_name, goods_icon, out_trade_no, ip, ",
            "terminal_type",
            "from payment_order",
            "where id = #{id,jdbcType=BIGINT} for update"
    })
    @ResultMap("BaseResultMap")
    Order selectByPrimaryKeyForUpdate(Long id);

}